package com.edu.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.edu.common.core.domain.entity.SysDept;
import com.edu.common.utils.DateUtils;
import com.edu.common.utils.StringUtils;
import com.edu.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.system.mapper.SysDeptPathMapper;
import com.edu.common.core.domain.entity.SysDeptPath;
import com.edu.system.service.ISysDeptPathService;

/**
 * 部门路径表Service业务层处理
 * 
 * @author zqq
 * @date 2023-01-18
 */
@Service
public class SysDeptPathServiceImpl implements ISysDeptPathService 
{
    @Autowired
    private SysDeptPathMapper sysDeptPathMapper;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询部门路径表
     * 
     * @param conditions 部门路径表主键
     * @return 部门路径表
     */
    @Override
    public SysDeptPath selectSysDeptPathById(Map<String,Object> conditions)
    {
        return sysDeptPathMapper.selectSysDeptPathById(conditions);
    }

    /**
     * 查询部门路径表列表
     * 
     * @param sysDeptPath 部门路径表
     * @return 部门路径表
     */
    @Override
    public List<SysDeptPath> selectSysDeptPathList(Map<String,Object> sysDeptPath)
    {
        return sysDeptPathMapper.selectSysDeptPathList(sysDeptPath);
    }

    /**
     * 新增部门路径表
     * 
     * @param sysDeptPath 部门路径表
     * @return 结果
     */
    @Override
    public int insertSysDeptPath(SysDeptPath sysDeptPath)
    {
        sysDeptPath.setCreateTime(DateUtils.getTime());
        return sysDeptPathMapper.insertSysDeptPath(sysDeptPath);
    }

    /**
     * 修改部门路径表
     * 
     * @param sysDeptPath 部门路径表
     * @return 结果
     */
    @Override
    public int updateSysDeptPath(SysDeptPath sysDeptPath)
    {
        sysDeptPath.setUpdateTime(DateUtils.getTime());
        return sysDeptPathMapper.updateSysDeptPath(sysDeptPath);
    }

    /**
     * 批量删除部门路径表
     * 
     * @param ids 需要删除的部门路径表主键
     * @return 结果
     */
    @Override
    public int deleteSysDeptPathByIds(Long[] ids)
    {
        return sysDeptPathMapper.deleteSysDeptPathByIds(ids);
    }

    /**
     * 删除部门路径表信息
     * 
     * @param id 部门路径表主键
     * @return 结果
     */
    @Override
    public int deleteSysDeptPathById(Long id)
    {
        return sysDeptPathMapper.deleteSysDeptPathById(id);
    }

    //单部门路径更新要根据父级部门更新
    @Override
    public void dealOnlyDeptPath(String corpId,String deptId,String parentId)
    {
        Map<String,Object> conditions = new HashMap<>();
        conditions.put("parentId",parentId);
        conditions.put("companyId",corpId);
        SysDeptPath deptPathInfo = selectSysDeptPathById(conditions);
        Map<String,Object> where = new HashMap<>();
        where.put("deptId",deptId);
        where.put("companyId",corpId);
        SysDept deptInfo = deptService.selectDeptById(where);

        List<Map<String,Object>> insertList = new ArrayList<>();
        Map<String,Object> path = new HashMap<>();
        path.put("deptId",deptId);
        path.put("deptName",deptInfo.getDeptName());
        path.put("parentId",deptInfo.getParentId());
        path.put("deptIdParentPath",deptPathInfo.getDeptIdParentPath()+ ","+ deptId);
        path.put("deptNameParentPath",deptPathInfo.getDeptNameParentPath()+ ","+deptInfo.getDeptName());
        path.put("status",1);
        path.put("isDelete",0);
        path.put("createTime", DateUtils.getNowDate());
        path.put("updateTime", DateUtils.getNowDate());
        path.put("companyId",corpId);
        insertList.add(path);
        if (!insertList.isEmpty()){
            sysDeptPathMapper.insertSysDeptPathAll(insertList);
        }
    }
}
